package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.AddressVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    MallUserMapper mallUserMapper;

    @Autowired
    MallAddressMapper mallAddressMapper;

    @Autowired
    MallRegionMapper mallRegionMapper ;

    @Autowired
    MallCollectMapper mallCollectMapper;

    @Autowired
    MallFootprintMapper mallFootprintMapper ;

    @Autowired
    MallSearchHistoryMapper mallSearchHistoryMapper ;

    @Autowired
    MallFeedbackMapper mallFeedbackMapper ;


    @Override
    public BaseData<MallUser> queryUser(UserParam param) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(),param.getLimit());

        MallUserExample mallUserExample = new MallUserExample();

        //根据username和mobile是否为null构造查询条件
        //根据sort和order去写排序
        //里面包含List<Criterion> 条件列表
        MallUserExample.Criteria criteria = mallUserExample.createCriteria();

        if (param.getMobile() != null) {
            criteria.andMobileLike("%" +param.getMobile()+"%");
        }

        if (param.getUsername() != null) {
            criteria.andUsernameLike("%" + param.getUsername() + "%");
        }

        mallUserExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallUser> users = mallUserMapper.selectByExample(mallUserExample);

        //total可以使用countByExample，也可以使用PageHelper来获得
        //int total = mallUserMapper.countByExample(mallUserExample);

        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<MallUser> pageInfo = new PageInfo<>(users);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(users, total);
    }

    @Override
    public Integer queryUserByMobilePhoneOrUsername(String mobilePhone, String username) {
        // 返回2，代表username已经被注册
        MallUserExample example2 = new MallUserExample();
        MallUserExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andUsernameEqualTo(username);
        List<MallUser> mallUsers2 = mallUserMapper.selectByExample(example2);
        if (mallUsers2 != null && mallUsers2.size() == 1) {
            return 2;
        }
        // 返回1，代表mobile已经被注册
        MallUserExample example = new MallUserExample();
        MallUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(mobilePhone);
        List<MallUser> mallUsers = mallUserMapper.selectByExample(example);
        if (mallUsers != null && mallUsers.size() == 1) {
            return 1;
        }
        return 3;
    }


    @Override
    public BaseData<AddressVO> queryaddress(AddressParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());

        MallAddressExample mallAddressExample = new MallAddressExample();
        MallAddressExample.Criteria criteria = mallAddressExample.createCriteria();

        if (param.getUserId() != null){
            criteria.andUserIdEqualTo(param.getUserId());
        }

        if (param.getName() != null){
            criteria.andNameLike("%" + param.getName() + "%");
        }

        mallAddressExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallAddress> mallAddresses = mallAddressMapper.selectByExample(mallAddressExample);

        PageInfo<MallAddress> pageInfo = new PageInfo<>(mallAddresses);
        int total = (int) pageInfo.getTotal();

        ArrayList<AddressVO> addressVOS = new ArrayList<>();

        for (MallAddress mallAddress : mallAddresses) {
            Integer areaId = mallAddress.getAreaId();
            Integer provinceId = mallAddress.getProvinceId();
            Integer cityId = mallAddress.getCityId();
            String area = mallRegionMapper.selectAddressById(areaId);   // 查找地区
            String province = mallRegionMapper.selectAddressById(provinceId);
            String city = mallRegionMapper.selectAddressById(cityId);
            AddressVO addressVO = new AddressVO(area,mallAddress.getIsDefault(),mallAddress.getAreaId(),mallAddress.getAddress(),province,city,mallAddress.getName(),mallAddress.getMobile(),mallAddress.getId(),mallAddress.getCityId(),mallAddress.getUserId(),mallAddress.getProvinceId());
            addressVOS.add(addressVO);
        }
        return BaseData.ok(addressVOS,total);
    }

    @Override
    public BaseData<MallCollect> queryenshrine(CollectParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallCollectExample mallCollectExample = new MallCollectExample();
        MallCollectExample.Criteria criteria = mallCollectExample.createCriteria();
        if (param.getUserId() != null){
            criteria.andUserIdEqualTo(param.getUserId());
        }
        if (param.getValueId() != null){
            criteria.andValueIdEqualTo(param.getValueId());
        }
        mallCollectExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallCollect> collects = mallCollectMapper.selectByExample(mallCollectExample);
        PageInfo<MallCollect> pageInfo = new PageInfo<>(collects);
        int total = (int) pageInfo.getTotal();
        return BaseData.ok(collects, total);
    }

    @Override
    public BaseData<MallFootprint> queryfoot(FootParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallFootprintExample mallFootprintExample = new MallFootprintExample();
        MallFootprintExample.Criteria criteria = mallFootprintExample.createCriteria();
        if (param.getUserId() != null){
            criteria.andUserIdEqualTo(param.getUserId());
        }
        if (param.getGoodsId() != null){
            criteria.andGoodsIdEqualTo(param.getGoodsId());
        }
        mallFootprintExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallFootprint> collects = mallFootprintMapper.selectByExample(mallFootprintExample);
        PageInfo<MallFootprint> pageInfo = new PageInfo<>(collects);
        int total = (int) pageInfo.getTotal();
        return BaseData.ok(collects, total);
    }

    @Override
    public BaseData<MallSearchHistory> queryhistory(HistoryParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallSearchHistoryExample mallSearchHistoryExample = new MallSearchHistoryExample();
        MallSearchHistoryExample.Criteria criteria = mallSearchHistoryExample.createCriteria();
        if (param.getUserId() != null){
            criteria.andUserIdEqualTo(param.getUserId());
        }
        if (param.getKeyword() != null){
            criteria.andKeywordLike("%" + param.getKeyword() + "%");
        }
        mallSearchHistoryExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallSearchHistory> collects = mallSearchHistoryMapper.selectByExample(mallSearchHistoryExample);
        PageInfo<MallSearchHistory> pageInfo = new PageInfo<>(collects);
        int total = (int) pageInfo.getTotal();
        return BaseData.ok(collects, total);
    }

    @Override
    public BaseData<MallFeedback> queryfeedback(FeedBackParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallFeedbackExample mallFeedbackExample = new MallFeedbackExample();
        MallFeedbackExample.Criteria criteria = mallFeedbackExample.createCriteria();
        if (param.getUsername() != null){
            criteria.andUsernameLike("%"+param.getUsername()+"%");
        }
        if (param.getId() != null){
            criteria.andIdEqualTo(param.getId());
        }
        mallFeedbackExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallFeedback> collects = mallFeedbackMapper.selectByExample(mallFeedbackExample);
        PageInfo<MallFeedback> pageInfo = new PageInfo<>(collects);
        int total = (int) pageInfo.getTotal();
        return BaseData.ok(collects, total);
    }

    @Override
    public MallUser queryUserByMobile(String mobile) {
        MallUserExample example = new MallUserExample();
        MallUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<MallUser> mallUsers = mallUserMapper.selectByExample(example);
        if (mallUsers.size() == 1) {
            return mallUsers.get(0);
        }
        return null;
    }

    @Override
    public Integer updateUser(MallUser user) {
        Integer status = mallUserMapper.updateByPrimaryKeySelective(user);
        return status;
    }

    @Override
    public Integer insertUser(MallUser user) {
        int status = mallUserMapper.insertSelective(user);
        return status;
    }


}
